<?php

namespace app\service;

use app\exception\ErrnoPlus;
use app\model\AliyunCloudStorageModel;
use share\exception\ApiException;
use share\service\BaseModelService;
use share\service\NotifyDingTalkService;
use think\Log;

class AliyunCloudStorageService extends BaseModelService
{

    const DEFAULT_CLOUD_RESOURCE =  "ilopLVCloudResource";

    const DEFAULT_EVENT_RESOURCE=  "eventResource_30";

    public static function getModel()
    {
        return new AliyunCloudStorageModel;
    }

    /**
     * 编辑条目 新增或者修改
     */
    public static function edit($info)
    {
        $id = $info['id'];
        unset($info['id']);
        if ($id == 0) {
            self::addAliyunCloudStorage($info);
        } else {
            self::updateAliyunCloudStorage($id, $info);
        }
        //TODO add code here after data change
        return $info;
    }

    /**
     * 新增条目
     */
    public static function addAliyunCloudStorage($info)
    {
        //TODO add code here  
        self::add($info);
    }

    /**
     * 修改条目
     */
    public static function updateAliyunCloudStorage($id, $info)
    {
        $origin  = self::getBase($id);
        if (empty($origin)) {
            throw new ApiException("origin data is not exists", ErrnoPlus::COMMON_INVALID_INPUT);
        }
        //TODO  add code here  

        self::update($id, $info);
    }

    /**
     * 删除条目
     */
    public static function delById($id)
    {
        $origin = self::getBase($id);
        if (empty($origin)) {
            return;
        }
        //TODO  add code here  
        $res = self::del($id);
        return $res;
    }

    /**
     * 虚拟的免费套餐类型
     */
    public static function getFreeAll(){
        return [
            [
                'id' => 80000,
                'commodity_name' => '免费循环事件云存储',
                'commodity_code' => 'freeIlopLVCloudResource',
                'specification' => 'eventResource_7_free',
                'record_type' => 2,
                'price' => 0,
                'lifecycle' => 7,
                'months' => 1,
                'site' => 'CN',
            ],
            [
                'id' => 80001,
                'commodity_name' => '免费循环事件云存储',
                'commodity_code' => 'freeIlopLVCloudResource',
                'specification' => 'eventResource_7_free',
                'record_type' => 2,
                'price' => 0,
                'lifecycle' => 7,
                'months' => 1,
                'site' => 'US',
            ],
        ];
    }

    /**
     * 从远程更新设备
     */
    public static function updateByRemote($site,$item){
        $origin = self::getByFilter([
            'commodity_code' => $item['commodity_code'],
            'specification' => $item['specification'],
            'site' => $site,
        ]);

        if($origin){
            $isSame  = true;
            foreach($item as $key => $val){
                if($origin[$key] != $val){
                    $isSame = false;
                    break;
                }
            }
            if($isSame){
                return;
            }
            else{
                $message = "aliyunCloudStorage Changed \norigin:".json_encode($origin,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);
                $message .= "\n new:".json_encode($item,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);
                trace($message,Log::ERROR);
                NotifyDingTalkService::pushTextByAMQP($message);
                self::update($origin['id'],$item);
            }
        }
        else{
            $item['site'] = $site;
            $message = "aliyunCloudStorage add new record \n data:".json_encode($item,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);
            self::add($item);
        }
    }

    /**
     * 页面查询条件
     */
    public static function buildCondition($filter)
    {
        $res = [];
        if (isset($filter['name']) && !empty($filter['name'])) {
            $res['name'] = ['like', '%' . $filter['name'] . '%'];
        }
        if (isset($filter['type']) && !empty($filter['type'])) {
            $res['type'] = $filter['type'];
        }
        //TODO  add code here  
        return $res;
    }

    public static function siteMap(){
        return  [
            'CN' => 1,
            'SG' => 2,
            'US' => 3,
            'DE' => 4,
        ];
    }

    public static function getSiteNum($site){
        $key = strtoupper(trim($site));
        $siteMap = self::siteMap();
        return isset($siteMap[$key]) ? $siteMap[$key] : 1;
    }
}
